<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl wix"
                >
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:key name="dirKey" match="wix:Component[./wix:File]" use="@Directory" />
  <xsl:template match="wix:ComponentGroup[./wix:Component/wix:File]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:text>&#xa;</xsl:text>
      <xsl:for-each select="wix:Component[ generate-id() = generate-id( key('dirKey', @Directory)[1])]">
        <wix:Component>
          <xsl:attribute name="Id">
            <xsl:value-of select="./@Id" />
          </xsl:attribute>
          <xsl:attribute name="Guid">
            <xsl:value-of select="./@Guid" />
          </xsl:attribute>
          <xsl:attribute name="Directory">
            <xsl:value-of select="./@Directory" />
          </xsl:attribute>
          <xsl:text>&#xa;</xsl:text>
          <wix:CreateFolder />
          <xsl:for-each select="key('dirKey', @Directory)">
            <xsl:apply-templates></xsl:apply-templates>
          </xsl:for-each>
        </wix:Component>
      </xsl:for-each>
      <xsl:text>&#xa;</xsl:text>
    </xsl:copy>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <xsl:template match="@KeyPath">    
  </xsl:template>
  
</xsl:stylesheet>